package ee.siimkaspar.subjects.model;


import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

import ee.siimkaspar.subjects.service.ContactService;

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@Table(name = "contact")
public class Contact implements Serializable, ModelInterface {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "contact", unique = true, nullable = false)
	private Integer id;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "contact_type_fk")
	private ContactType contactType;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "subject_type_fk")
	private SubjectType subjectType;
	@Column(name = "subject_fk")
	private Integer subjectFk; //pointer to Person(subjectType.id==1) or Enterprise(subjectType.id==2)
	@Column(name = "value_text")
	private String valueText;
	@Column(name = "orderby")
	private Integer orderby;
	@Column(name = "note")
	private String note;

	public Contact() {
	}
	
	public void reAttachMissingData(ContactService cService) {
		Contact contact = cService.findObject(Contact.class, id);
		if (contact != null) {
			this.contactType = contact.getContactType();
			this.subjectType = contact.getSubjectType();
			this.subjectFk = contact.getSubjectFk();
			this.orderby= contact.getOrderby();
			this.note = contact.getNote();
		}
	}

	@Override
	public Integer getId() {
		return id;
	}

	@Override
	public void setId(Integer id) {
		this.id = id;	
	}

	public ContactType getContactType() {
		return this.contactType;
	}

	public void setContactType(ContactType contactType) {
		this.contactType = contactType;
	}

	public SubjectType getSubjectType() {
		return this.subjectType;
	}

	public void setSubjectType(SubjectType subjectType) {
		this.subjectType = subjectType;
	}

	public Integer getSubjectFk() {
		return this.subjectFk;
	}

	public void setSubjectFk(Integer subjectFk) {
		this.subjectFk = subjectFk;
	}

	public String getValueText() {
		return this.valueText;
	}

	public void setValueText(String valueText) {
		this.valueText = valueText;
	}

	public Integer getOrderby() {
		return this.orderby;
	}

	public void setOrderby(Integer orderby) {
		this.orderby = orderby;
	}

	public String getNote() {
		return this.note;
	}

	public void setNote(String note) {
		this.note = note;
	}
	@Override
	public boolean validate() {
		// TODO Auto-generated method stub
		return true;
	}
}
